﻿Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports System.Windows.Forms
Public Class inicio_evaluacion
    Public Function AutoCompletar(ByVal Control As TextBox) As AutoCompleteStringCollection
        Dim Coleccion As New AutoCompleteStringCollection

        Dim Comando As String

        Comando = "SELECT rtrim(RUC) FROM Clientes ORDER By RUC asc"
        'Creamos una nueva cadena de coneccion 

        Using Coneccion As New SqlConnection(nCnn)
            Dim Ejecutar As New SqlClient.SqlCommand(Comando, Coneccion)

            'Abrimos la coneccion

            Coneccion.Open()
            Dim Lector As SqlDataReader = Ejecutar.ExecuteReader()

            ' Realizamos un Loop mientras se est‚ leyendo.
            While Lector.Read()
                Coleccion.AddRange(New String() {Lector(0)})
            End While
            'Cerramos el SqlReader
            Lector.Close()

            'Cerramos la coneccion
            Coneccion.Close()

        End Using
        'Ajustamos el control TextBox o ComboBox para recibir los datos de la siguiente manera.
        With Control
            .AutoCompleteMode = AutoCompleteMode.Suggest

            .AutoCompleteSource = AutoCompleteSource.CustomSource

            .AutoCompleteCustomSource = Coleccion

        End With
        'Devolvemos los datos recuperados de la base de datos
        Return Coleccion

    End Function
    Public Function AutoCompletar1(ByVal Control As TextBox) As AutoCompleteStringCollection
        Dim Coleccion As New AutoCompleteStringCollection

        Dim Comando As String

        Comando = "SELECT rtrim(DESCLI) FROM Clientes ORDER By DESCLI asc"
        'Creamos una nueva cadena de coneccion 

        Using Coneccion As New SqlConnection(nCnn)
            Dim Ejecutar As New SqlClient.SqlCommand(Comando, Coneccion)

            'Abrimos la coneccion

            Coneccion.Open()
            Dim Lector As SqlDataReader = Ejecutar.ExecuteReader()

            ' Realizamos un Loop mientras se est‚ leyendo.
            While Lector.Read()
                Coleccion.AddRange(New String() {Lector(0)})
            End While
            'Cerramos el SqlReader
            Lector.Close()

            'Cerramos la coneccion
            Coneccion.Close()

        End Using
        'Ajustamos el control TextBox o ComboBox para recibir los datos de la siguiente manera.
        With Control
            .AutoCompleteMode = AutoCompleteMode.Suggest

            .AutoCompleteSource = AutoCompleteSource.CustomSource

            .AutoCompleteCustomSource = Coleccion

        End With
        'Devolvemos los datos recuperados de la base de datos
        Return Coleccion

    End Function
    Public Sub Enviar_Tab(ByVal Ctl As Control, ByVal Tecla As Integer)
        If Tecla = 13 Then
            Dim SQL As String = "select  DESCLI from Clientes where RUC='" & TextBox1.Text & "'"
            Dim da As SqlDataAdapter
            Dim dt As New DataTable
            Try
                da = New SqlDataAdapter(SQL, nCnn)
                da.Fill(dt)
                If dt.Rows.Count <> 0 Then
                    TextBox2.Text = RTrim(dt.Rows(0)("DESCLI"))
                    Button1.Enabled = True
                    Button1.Focus()
                Else
                    MsgBox("El RUC ingresado no existe", MsgBoxStyle.Critical)
                    TextBox1.Text = ""
                    Button1.Enabled = False
                    TextBox1.Focus()
                End If
            Catch ex As Exception

            End Try
            'SendKeys.Send("{Tab}")
        End If
    End Sub
    Public Sub Enviar_Tab1(ByVal Ctl As Control, ByVal Tecla As Integer)
        If Tecla = 13 Then
            Dim cadena As String = TextBox2.Text
            cadena = cadena.Replace("'", "''")
            Dim SQL As String = "select RUC from Clientes where DESCLI='" & cadena & "'"
            Dim da As SqlDataAdapter
            Dim dt As New DataTable
            Try
                da = New SqlDataAdapter(SQL, nCnn)
                da.Fill(dt)
                If dt.Rows.Count <> 0 Then
                    TextBox1.Text = RTrim(dt.Rows(0)("RUC"))
                    Button1.Enabled = True
                    Button1.Focus()
                Else
                    MsgBox("El cliente ingresado no existe", MsgBoxStyle.Critical)
                    TextBox2.Text = ""
                    Button1.Enabled = False
                    TextBox2.Focus()
                End If
            Catch ex As Exception

            End Try
            'SendKeys.Send("{Tab}")
        End If

    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Len(TextBox1.Text) <> 0 And Len(TextBox2.Text) <> 0 Then
            Ruc = TextBox1.Text
            Cliente = TextBox2.Text
            evaluacion_clientes.MdiParent = menu_principal
            evaluacion_clientes.Show()
            Me.Close()
        Else
            MsgBox("Ruc y/o Razón Social incorrectos", MsgBoxStyle.Critical)
            TextBox1.Text = ""
            TextBox2.Text = ""
            TextBox1.Focus()
        End If
    End Sub

    Private Sub inicio_evaluacion_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        AutoCompletar(Me.TextBox1)
        AutoCompletar1(Me.TextBox2)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub

    Private Sub TextBox2_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox2.KeyDown
        Enviar_Tab1(sender, e.KeyCode)
    End Sub

    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged

    End Sub

    Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
        Enviar_Tab(sender, e.KeyCode)
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    End Sub
End Class